﻿// using System.Diagnostics;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

/// <summary>
/// 转发状态机消息到 animator,以便使用自定义的 FSM逻辑代码
/// </summary>
public class FSMMessagePass : StateMachineBehaviour
{
    public string StateName = "";
    public bool enter = true;
    public bool update = false;
    public bool exit = false;

    private string GetStringUpper(string s)
    {
        return s.Substring(0, 1).ToUpper() + s.Substring(1);
    }

    // OnStateEnter is called when a transition starts and the state machine starts to evaluate this state
    override public void OnStateEnter(Animator animator, AnimatorStateInfo stateInfo, int layerIndex)
    {
        if (string.IsNullOrEmpty(StateName) || !enter) return;
        // Debug.Log("enter:"+StateName);
        animator.transform.parent.SendMessage($"On{GetStringUpper(StateName)}Enter");
    }

    // OnStateUpdate is called on each Update frame between OnStateEnter and OnStateExit callbacks
    override public void OnStateUpdate(Animator animator, AnimatorStateInfo stateInfo, int layerIndex)
    {
        if (string.IsNullOrEmpty(StateName) || !update) return;
        animator.transform.parent.SendMessage($"On{GetStringUpper(StateName)}Update");
        // GlobalEvent.Dispatcher("FSM:On" + StateName + "Update");
    }

    // OnStateExit is called when a transition ends and the state machine finishes evaluating this state
    override public void OnStateExit(Animator animator, AnimatorStateInfo stateInfo, int layerIndex)
    {
        if (string.IsNullOrEmpty(StateName) || !exit) return;
        // Debug.Log("exit:"+StateName);
        animator.transform.parent.SendMessage($"On{GetStringUpper(StateName)}Exit");
        // GlobalEvent.Dispatcher("FSM:On" + StateName + "Exit");
    }

    // OnStateMove is called right after Animator.OnAnimatorMove()
    //override public void OnStateMove(Animator animator, AnimatorStateInfo stateInfo, int layerIndex)
    //{
    //    // Implement code that processes and affects root motion
    //}

    // OnStateIK is called right after Animator.OnAnimatorIK()
    //override public void OnStateIK(Animator animator, AnimatorStateInfo stateInfo, int layerIndex)
    //{
    //    // Implement code that sets up animation IK (inverse kinematics)
    //}
}
